package cn.yuemouren.tree.segment.leetcode;

/**
 * @Author: Timi
 * @Description:
 *      解题思路：
 *          因为数组值固定，所以可以使用另一个数组保存索引为n，代表前n项的和
 * @Date: 2020/7/9 8:53
 */
public class ValidParenthesis_303_2 {

    private int[] sum;
    public ValidParenthesis_303_2(int[] nums) {
        if(nums.length > 0){
            //sum[0] = 0;
            sum = new int[nums.length + 1];
            for(int i = 0; i < nums.length; i++)
                sum[i + 1] = sum[i] + nums[i];
        }
    }

    public int sumRange(int i, int j) {
        return sum[j+1] - sum[i];
    }

    public static void main(String[] args) {
        int[] nums = {-2, 0, 3, -5, 2, -1};
        ValidParenthesis_303_2 dto = new ValidParenthesis_303_2(nums);
        int i = dto.sumRange(0, 5);
        System.out.println(i);
    }
}
